package com.ext.aic.adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.ext.aic.R;
import com.ext.aic.beans.SecondIndustry;
import com.ext.aic.databinding.IndustryListItemViewBinding;
import com.ext.aic.holder.IndustryViewHolder;
import com.ext.aic.ui.IndustryEnterpriseListActivity;
import com.ext.aic.utils.RedirectUtils;
import com.eyinfo.android_pure_utils.events.Action1;
import com.eyinfo.android_pure_utils.utils.ConvertUtils;
import com.eyinfo.android_pure_utils.utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;

import lombok.Setter;

@Setter
public class IndustryAdapter extends RecyclerView.Adapter<IndustryViewHolder> {

    private List<SecondIndustry> items = new ArrayList<>();
    private Action1<SecondIndustry> itemCall;

    @Override
    public IndustryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.industry_list_item_view, parent, false);
        return new IndustryViewHolder(IndustryListItemViewBinding.bind(view));
    }

    @Override
    public void onBindViewHolder(IndustryViewHolder holder, int position) {
        SecondIndustry secondIndustry = items.get(position);
        holder.industryItemRl.setTag(position);
        holder.industryItemRl.setOnClickListener(this::onItemClick);
        holder.industryTv.setText(secondIndustry.getName());
        holder.industryDescTv.setText(secondIndustry.getInfo());
    }

    public void onItemClick(View view) {
        int position = ConvertUtils.toInt(view.getTag());
        SecondIndustry secondIndustry = items.get(position);
        if (itemCall != null) {
            itemCall.call(secondIndustry);
        }
        String json = JsonUtils.toJson(secondIndustry);
        Bundle bundle = new Bundle();
        bundle.putString("industry", json);
        RedirectUtils.getInstance().startActivity(view.getContext(), IndustryEnterpriseListActivity.class, bundle);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}
